﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mani.Common.Security;
using RastinArgham.CRM.Business;
using Mani.Common;

namespace RastinArgham.CRM.Web
{
    public partial class list_services : System.Web.UI.Page
    {
        public ISecurityManager securityManager { get; set; }
        public CRMRepository crmRepository { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (securityManager == null)
            {
                securityManager = new SecurityManager();
                securityManager.authProvider = new WebAuthorizationProvider();
            }

            if (crmRepository == null)
                crmRepository = new CRMRepository();


            if (!securityManager.IsActionPermitted("services"))
                Response.Redirect("~/access_denied.aspx");

            if (!Page.IsPostBack)
            {
                LoadProvinces();
                LoadServices();
            }
        }

        private void LoadProvinces()
        {
            ddlProvinces.DataSource = crmRepository.GetProvinces();
            ddlProvinces.DataBind();
        }

        private void LoadServices()
        {
            gvServices.DataSource = crmRepository.GetServices();
            gvServices.DataBind();
        }

        protected void btnSaveChanges_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txtId.Text))
                New();
            else
                Update();

            UIHelper.ClientAlert(this, "تغییرات با موفقیت ذخیره شد");
            ClearForm();
            LoadServices();
        }

        private void New()
        {
            Service s = new Service();
            s.OperatorId = securityManager.authProvider.GetCurrentUser().UserId;
            s.Description = txtDescription.Text;
            s.IsCertificateRequired = cbIsCertificateRequired.Checked;
            s.Enable = cbEnable.Checked;
            s.Online = cbOnline.Checked;
            s.Price = Convert.ToInt64( UIHelper.RemoveFormat(txtPrice.Text) );
            s.MobinServiceName = txtMobinServiceName.Text;

            if (ddlProvinces.SelectedValue != "-1")
                s.Province = crmRepository.GetProvinceById(Convert.ToInt32(ddlProvinces.SelectedValue));

            s.ServiceName = txtServiceName.Text;
            s.ServicePeriod = Convert.ToInt32( ddlServicePeriod.SelectedValue );
            s.TaxPercent = Convert.ToDouble(txtTaxPercent.Text);

            crmRepository.AddService(s);
            crmRepository.Save();
        }

        private void Update()
        {
            Service s = crmRepository.GetServiceById(Convert.ToInt32(txtId.Text));
            
            s.OperatorId = securityManager.authProvider.GetCurrentUser().UserId;
            s.Description = txtDescription.Text;
            s.IsCertificateRequired = cbIsCertificateRequired.Checked;
            s.Enable = cbEnable.Checked;
            s.Online = cbOnline.Checked;
            s.Price = Convert.ToInt64(UIHelper.RemoveFormat(txtPrice.Text));
            s.MobinServiceName = txtMobinServiceName.Text;

            if (ddlProvinces.SelectedValue == "-1")
            {
                s.Province = null;
                s.ProvinceId = null;
            }
            else
            {
                s.Province = crmRepository.GetProvinceById(Convert.ToInt32(ddlProvinces.SelectedValue));
            }

            s.ServiceName = txtServiceName.Text;
            s.ServicePeriod = Convert.ToInt32(ddlServicePeriod.SelectedValue);
            s.TaxPercent = Convert.ToDouble(txtTaxPercent.Text);

            crmRepository.Save();
        }

        protected void gvServices_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            ClearForm();

            int serviceId = Convert.ToInt32(gvServices.DataKeys[e.NewSelectedIndex].Value);
            Service service = crmRepository.GetServiceById(serviceId);
            txtId.Text = service.ServiceId.ToString();
            txtServiceName.Text = service.ServiceName;
            txtDescription.Text = service.Description;
            txtPrice.Text = UIHelper.FormatString(service.Price);
            txtTaxPercent.Text = service.TaxPercent.ToString();
            txtMobinServiceName.Text = service.MobinServiceName;
            cbIsCertificateRequired.Checked = service.IsCertificateRequired;
            cbEnable.Checked = service.Enable;
            cbOnline.Checked = service.Online;
            ddlServicePeriod.Items.FindByValue(service.ServicePeriod.ToString()).Selected = true;

            if (service.ProvinceId.HasValue)
                ddlProvinces.Items.FindByValue(service.ProvinceId.Value.ToString()).Selected = true;
        }

        private void ClearForm()
        {
            txtId.Text = String.Empty;
            txtServiceName.Text = String.Empty;
            txtDescription.Text = String.Empty;
            txtPrice.Text = String.Empty;
            txtTaxPercent.Text = String.Empty;
            txtMobinServiceName.Text = String.Empty;
            cbEnable.Checked = false;
            cbIsCertificateRequired.Checked = false;
            ddlServicePeriod.ClearSelection();
            ddlProvinces.ClearSelection();
        }

        protected void gvServices_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvServices.PageIndex = e.NewPageIndex;
            LoadServices();
        }
    }
}